﻿using StudentLive.DataModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace StudentLive.InnerPages
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class FavoritesPage : StudentLive.Common.LayoutAwarePage
    {
        public FavoritesPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            this.DefaultViewModel["Items"] = LiveDataSource.GetFavoriteResources();            
        }

        private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            DefaultLaunch((e.ClickedItem as ResourceItem).LocalPath);
        }

        async void DefaultLaunch(string path)
        {
            // Path to the file in the app package to launch

            var file = await StorageFile.GetFileFromPathAsync(Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, path));

            if (file != null)
            {
                // Set the option to show the picker
                //var options = new Windows.System.LauncherOptions();
                //options.DisplayApplicationPicker = true;

                // Launch the retrieved file
                bool success = await Windows.System.Launcher.LaunchFileAsync(file);
                if (success)
                {
                    // File launched
                }
                else
                {
                    // File launch failed
                }
            }
            else
            {
                // Could not find file
            }
        }

        private void FavoriteButtonClick(object sender, RoutedEventArgs e)
        {
            var lr = ((ResourceItem)(itemGridView.SelectedItem));
            lr.IsFavoured = !lr.IsFavoured;
            LiveDataSource.UpdateLearningResource(lr);
            if (!lr.IsFavoured)
            {
                UnFavorButton.Visibility = Visibility.Collapsed;
                this.DefaultViewModel["Items"] = LiveDataSource.GetFavoriteResources();
            }
            BottomAppBar.IsOpen = false;
            BottomAppBar.Visibility = Visibility.Collapsed;

        }

        private void itemGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = itemGridView.SelectedItem;
            if (item != null)
            {
                BottomAppBar.Visibility = Visibility.Visible;
                BottomAppBar.IsOpen=true;
            }
            else
            {
                BottomAppBar.Visibility = Visibility.Collapsed;
            }
        }

    }
}
